package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result addShoppingCart(ShoppingCartDTO dto) {
        //1.非空判断
        if(dto==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2.判断当前菜品或者套餐是否存在
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
            //2.1 获取用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
            //2.2 调mapper接口
        shoppingCart = shoppingCarMapper.queryCart(shoppingCart);
        //3.如果存在，则更新数据库中菜品或者套餐的数量
        if(shoppingCart!=null){
            //3.2 设置数量
            shoppingCart.setNumber(shoppingCart.getNumber()+1);

            //3.1 调mapper接口
            shoppingCarMapper.updateCartNum(shoppingCart);
        }else{
            //4.如果不存在
            shoppingCart=new ShoppingCart();
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);

            //菜品添加
            if(dto.getDishId()!=null){
                shoppingCart.setDishId(dto.getDishId());
                shoppingCart.setDishFlavor(dto.getDishFlavor());
                //获取菜品名称，金额和图片
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }

            //套餐添加
            if(dto.getSetmealId()!=null){
                shoppingCart.setSetmealId(dto.getSetmealId());
                //获取套餐名称，金额和图片
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            //4.1 调mapper接口
            shoppingCarMapper.insertCart(shoppingCart);
        }
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public Result queryShoppingCart() {
        //1.获取登录人信息
        Long currentId = BaseContext.getCurrentId();
        //2.根据条件查询购物车mapper
        List<ShoppingCart> list = shoppingCarMapper.list(currentId);
        //3.返回数据
        return Result.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    @Transactional
    public Result cleanShoppingCart() {
        //1.获取登录人信息
        Long currentId = BaseContext.getCurrentId();
        //2.根据条件清空购物车mapper
        shoppingCarMapper.clean(currentId);
        //3.返回数据
        return Result.success();
    }
}
